package com.luoxiang.socialandroid.activity;

import static com.luoxiang.lib_common.common.Constants.MmkvConst.EXTRA;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.KEY;
import static com.luoxiang.lib_common.common.Constants.MmkvConst.TOKEN;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.google.gson.Gson;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.adapter.BlackAdapter;
import com.luoxiang.socialandroid.base.BaseActivity;
import com.luoxiang.socialandroid.bean.BlackBean;
import com.luoxiang.socialandroid.bean.NumberBean;
import com.luoxiang.socialandroid.utils.AESUtil;
import com.luoxiang.socialandroid.utils.MmkvUtil;
import com.luoxiang.socialandroid.utils.NetUtils2;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class BlackActivity extends BaseActivity {
    private ImageView icon_back;
    private TextView  tx_jiechu;
    private RecyclerView rc_black;
    private String token;
    private BlackAdapter adapter;
    private BlackBean bean;

    @Override
    protected void initListener() {
        icon_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        jiechu();
        cancelblock(token, EXTRA);
    }

    private void jiechu() {
        tx_jiechu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //实时获取控件的值
                String buttonText = tx_jiechu.getText().toString();
                if (buttonText.equals(getString(R.string.mi_jiechu))) {
                    tx_jiechu.setText(R.string.mi_wancheng);
                    adapter.setJieChuValue(true);
                    adapter.notifyDataSetChanged();
                } else {
                    tx_jiechu.setText(getString(R.string.mi_jiechu));
                    adapter.setJieChuValue(false);
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    protected void initLayout(Bundle savedInstanceState) {
        setContentView(R.layout.activity_black);
        setStatusBarTranslucent();
    }

    @Override
    protected void initView() {
        icon_back = findViewById(R.id.icon_back);
        rc_black = findViewById(R.id.rc_black);
        token = MmkvUtil.INSTANCE.decodeString(TOKEN);

        tx_jiechu = findViewById(R.id.tx_jiechu);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(BlackActivity.this);
        rc_black.setLayoutManager(linearLayoutManager);
        adapter = new BlackAdapter(null);
        rc_black.setAdapter(adapter);

        getBlacklist(token, EXTRA);
    }

    private void cancelblock(String token, String extra) {
        adapter.setOnAddClickListener(new BlackAdapter.OnAddClickListener() {
            @Override
            public void onItemClicks(int position) {
                NetUtils2.getInstance().getApiService().block(token, extra, bean.getData().getUsers().get(position).getId(), 0)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<NumberBean>() {
                            @Override
                            public void onSubscribe(Disposable d) {

                            }

                            @Override
                            public void onNext(NumberBean numberBean) {
                                String data = numberBean.getData();
                                String decryptStr = null;
                                try {
                                    decryptStr = AESUtil.aesDecryptString(data, KEY);
                                    LogUtils.i("decryptStr23", decryptStr);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                                getBlacklist(token, EXTRA);
                            }

                            @Override
                            public void onError(Throwable e) {

                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        });
    }

    private void getBlacklist(String token, String extra) {
        NetUtils2.getInstance().getApiService().getBlackList(token, extra)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<NumberBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(NumberBean numberBean) {
                        String data = numberBean.getData();
                        String decryptStr = null;
                        try {
                            decryptStr = AESUtil.aesDecryptString(data, KEY);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        LogUtils.i("decryptStr22", decryptStr);
                        Gson gson = new Gson();
                        bean = gson.fromJson(decryptStr, BlackBean.class);


                        if (bean.getData() != null) {
                            if (bean.getData().getUsers() != null || !bean.getData().getUsers().isEmpty()) {
                                adapter.setData(bean.getData().getUsers());
                                adapter.notifyDataSetChanged();

                            } else {

                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
